<template>
  <el-dialog
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="clickClose"
    @open="handleOpen"
    v-loading="loading"
  >
    <!-- 这里准备复选框 -->
    <!-- 
      将来使用el-checkbox-group这个组件获取值不在是value
      而是label获取返回值
     -->
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in rolsList" :label="item.id" :key="item.id">{{
        item.name
      }}</el-checkbox>
    </el-checkbox-group>

    <template #footer>
      <el-button type="primary" size="small" @click="submitBtn">确定</el-button>
      <el-button size="small" @click="clickClose">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { reqGetSole,reqAssignRoles } from '@/api/settings'
import { reqGetBaseUserInfo } from '@/api/user'

export default {
  data() {
    return {
      checkList: ['101', '102'],
      rolsList: [], //角色列表数据
      loading: false, //控制longding
    }
  },
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false,
    },
    // 用户的id 用来查询当前用户的角色信息
    userId: {
      type: String,
      default: null,
    },
  },
  created() {},
  methods: {
    async submitBtn(){
      const res = await reqAssignRoles({
        id:this.userId,
        roleIds:this.checkList
      })
      console.log(res,22222)
      this.$message.success('添加角色成功')
    },
    async getEmployDetail() {
      const {
        data:{roleIds}
      } = await reqGetBaseUserInfo(this.userId)
      this.checkList = roleIds
    },
    async handleOpen() {
      this.loading = true
      Promise.all([ this.getEmployDetail(), this.getRolsList()]).then(()=>{
         this.loading = false
      })
     
    },
    // 获取角色信息列表
    async getRolsList() {
      const {
        data: { rows },
      } = await reqGetSole(1, 9999)
      this.rolsList = rows
    },
    // 关闭弹窗 父组件放权
    clickClose() {
      this.$emit('update:showRoleDialog', false)
      // 清空数据
      this.checkList = ''
    },
  },
}
</script>